
#include "com_anim.h"
#include "page_manager.h"

static com_anim_t c_a = {
    .index = 0,
    .img   = NULL,
    .time  = NULL,
};

char* com_loading_img[] = {
    "Loading/Loading_00.png",
    "Loading/Loading_01.png",
    "Loading/Loading_02.png",
    "Loading/Loading_03.png",
    "Loading/Loading_04.png",

    "Loading/Loading_05.png",
    "Loading/Loading_06.png",
    "Loading/Loading_07.png",
    "Loading/Loading_08.png",
    "Loading/Loading_09.png",

    "Loading/Loading_10.png",
    "Loading/Loading_11.png",
    "Loading/Loading_12.png",
    "Loading/Loading_13.png",
    "Loading/Loading_14.png",  

    "Loading/Loading_15.png",
    "Loading/Loading_16.png",
    "Loading/Loading_17.png",  
};

int com_loading_res_init(void)
{
    int i = 0;
    char new_path[256];
    char normal_path[256];
    lv_img_header_t header;

    int fp = -1;
	unsigned int w, h;
	uint32_t sbuf_len = 0;
	unsigned char* dbuf = NULL;
	unsigned char* sbuf = NULL;

    unsigned char ucdata;
	unsigned char* p = NULL;
	int32_t t, j;
    int ret = -1;

	int32_t total_size = 0;

    for(i=0;i<LOADING_NUM;i++)
    {
        memset(normal_path,0,sizeof(normal_path));
        #ifdef PT_BK7258
        snprintf(normal_path,sizeof(normal_path),"/mnt/lfs/%s",com_loading_img[i]);
        #else
		snprintf(normal_path,sizeof(normal_path),"./res/%s",com_loading_img[i]);
        #endif

		memset(new_path,0,sizeof(new_path));
        #ifdef PT_BK7258
        snprintf(new_path,sizeof(new_path),"L:/mnt/lfs/%s",com_loading_img[i]);
        #else
		snprintf(new_path,sizeof(new_path),"L:./res/%s",com_loading_img[i]);
        #endif

        debug_d("new_path=%s\n",new_path);

    	ret = lv_img_decoder_get_info(new_path, &header);
        debug_d("ret=%d\n",ret);
		
        sbuf_len = logo_getFileSize(normal_path);
        if(!sbuf_len){
            continue;
        }
		
		sbuf = psram_malloc(sbuf_len);
		if(!sbuf){
			continue;
		}

		fp = open(normal_path,O_RDWR);
		if(fp < 0){
			free(sbuf);
			sbuf = NULL;
			continue;
		}

		read(fp,sbuf,sbuf_len);
		close(fp);
		fp = -1;

		ret = lv_png_get_argb_from_buff(&dbuf,sbuf,sbuf_len,&w,&h);
		if(ret){
			free(sbuf);
			sbuf = NULL;
			continue;
		}

		t = w * h;
		p = dbuf;

        debug_d("w:h=%d:%d\n",w,h);

        c_a.loading_img[i].header.cf  		   = header.cf;
    	c_a.loading_img[i].header.always_zero  = header.always_zero;
    	c_a.loading_img[i].header.reserved     = header.reserved;
    	c_a.loading_img[i].header.w  		   = header.w;
    	c_a.loading_img[i].header.h 		   = header.h;
    	c_a.loading_img[i].data_size		   = header.w*header.h*4;
    	c_a.loading_img[i].data 			   = (const uint8_t*)dbuf;

		total_size = total_size + header.w*header.h*4;

		free(sbuf);
		sbuf = NULL;
    }

    debug_d("loading_total_size=%dk\n",total_size/1024);

    return 0;
}

int com_loading_res_deinit(void)
{
    int i = 0;
    for(i=0;i<LOADING_NUM;i++)
    {
        if(c_a.loading_img[i].data){
            psram_free(c_a.loading_img[i].data);
            c_a.loading_img[i].data = NULL;
        }
    }

    return 0;
}

int com_anim_update(void)
{
    c_a.index++;
    if(c_a.index >= LOADING_NUM){
        c_a.index = 0; 

        if(c_a.user_cb){
            c_a.user_cb(c_a.user_data); 
        }
        com_anim_destroy();

        return 0;
    }

    // debug_d("index=%d\n",c_a.index);

    if(c_a.img){
        lv_img_set_src(c_a.img,(void*)(&c_a.loading_img[c_a.index]));
    }
    
    return 0;
}

void com_anim_timer_callback(lv_timer_t * t)
{   
    com_anim_update();
}

int com_anim_create(lv_obj_t* parent,lv_coord_t x,lv_coord_t y,anim_callback user_cb,void* user_data)
{
    if(c_a.img){
        return 0;
    }
    c_a.index = 0;
    c_a.img   = lv_img_create(parent);
    
    lv_obj_set_pos(c_a.img,x,y);

    c_a.time = lv_timer_create(com_anim_timer_callback,60,NULL);

    c_a.user_cb   = user_cb;
    c_a.user_data = user_data;

    return 0;
}

int com_anim_destroy(void)
{
    c_a.index = 0;
    if(c_a.img){
        lv_obj_del(c_a.img);
        c_a.img = NULL;
    }
    if(c_a.time){
        lv_timer_del(c_a.time);
        c_a.time = NULL;
    }

    c_a.user_cb   = NULL;
    c_a.user_data = NULL;

    return 0;
}

